package net.dlz.example.controller;


import net.dlz.example.domain.Application;
import net.dlz.example.service.ApplicationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;

import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@RequestMapping("/applications")
public class ApplicationController {
    @Autowired
    private ApplicationService applicationService;

    /**
     * 查询所有申请者的信息
     *
     * @param model
     * @return
     */
    @RequestMapping(value = "/search", method = RequestMethod.GET)
    public String applicationPage(Model model) {
        List<Application> applicationList = applicationService.findAllApplication0();
        model.addAttribute("applications", applicationList);
        return "application-list";
    }

    /**
     * 用户提交申请数据
     *
     * @param model
     * @return
     */
    @RequestMapping(value = "/add", method = RequestMethod.GET)
    public String addApplication(Model model) {
        model.addAttribute("applications", new Application());
        return "application_add";

    }


    /**
     * 用户提交跳转，要求用户不能提交空值，提交空值，给出警告，不会跳转
     *
     * @param application
     * @return
     */
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public String saveApplication(Application application, String userId, ModelMap modelMap, Model model) {
        Application userid = applicationService.findApplicationByUserId(userId);
        String msgs = null;
        if (application.getUserId() == "" || application.getApplicationThings() == "") {
            return "redirect:/applications/add";
        } else if (userid != null) {
            msgs = "该用户已经提交，请重新输入！";
            modelMap.put("msgs", msgs);
            model.addAttribute("applications", new Application());
            return "application_add";

        } else {
            int update = applicationService.saveApplication(application);
            msgs = "提交成功！";
            modelMap.put("msgs", msgs);
            model.addAttribute("applications", new Application());
            return "application_add";
        }


    }

    /**
     * 修改审核意见后存储避免和上面的用户申请冲突
     *
     * @param application
     * @return
     */
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public String saveApplication1(Application application) {
        int update = applicationService.saveApplication(application);
        return "redirect:/applications/search";
    }

    /**
     * 管理员审批管理--通过和不通过
     *
     * @param id
     * @return
     */
    @RequestMapping("/pass")
    @ResponseBody
    public String pass(@RequestParam("id") String id) {
        Application application = applicationService.findApplicationById(id);
        application.setApplicationbsck("审核通过");
        int update = applicationService.saveApplication(application);
        return "ok";
    }

    @RequestMapping("/reject")
    @ResponseBody
    public String reject(String id) {
        Application application = applicationService.findApplicationById(id);
        application.setApplicationbsck("审核不通过");
        int update = applicationService.saveApplication(application);
        return "redirect:/application/search";
    }


    /**
     * 管理员添加审核意见
     *
     * @param id
     * @param model
     * @return
     */

    @GetMapping("/amend")
    public String amendApplication(@RequestParam("id") String id, Model model) {
        Application application = applicationService.findApplicationById(id);
        application.setUserId(application.getUserId());
        model.addAttribute("applications", application);
        return "application-amend";
    }

    @RequestMapping("/tofind")
    public String seek() {
        return "application-look";
    }

    /**
     * 用户在前端查询审核状态
     * 根据userId查询该user的申请状态并将值传到前端
     *
     * @param model
     * @param userId
     * @return
     */
    @RequestMapping("/find")
    public String seekApply(Model model, String userId) {
        Application application = applicationService.findApplicationByUserId(userId);
        model.addAttribute("userId", userId);

        if (application != null)
            model.addAttribute("applicationb", application.getApplicationbsck());
        else {
            model.addAttribute("applicationb", "该id不存在！");
        }
        return "application-look";

    }


    /**
     ************** 门牌补发
    * */



    /**
     * 查询所有门牌补发申请者的信息
     *
     * @param model
     * @return
     */
    @RequestMapping(value = "/search2", method = RequestMethod.GET)
    public String applicationPage2(Model model) {
        List<Application> applicationList = applicationService.findAllApplication1();
        model.addAttribute("applications", applicationList);
        return "application-list2";
    }

    /**
     * 用户提交门牌补发申请数据
     *
     * @param model
     * @return
     */
    @RequestMapping(value = "/add2", method = RequestMethod.GET)
    public String addApplication2( Model model) {
        model.addAttribute("applications", new Application());
        return "application_add2";

    }


    /**
     * 用户提交跳转，要求用户不能提交空值，提交空值，给出警告，不会跳转
     *
     * @param application
     * @return
     */
    @RequestMapping(value = "/add2", method = RequestMethod.POST)
    public String saveApplication2(Application application,String userId ,ModelMap modelMap,Model model) {
      Application  application1=applicationService.findApplicationByUserId2(userId);
        String msgs=null;
        if(application.getUserId()=="" || application.getApplicationThings()==""){

            return "application_add2";
        }else if (application1 == null ){
            msgs = "该用户不存在，请重新输入！";
            modelMap.put("msgs", msgs);
            model.addAttribute("applications", new Application());
            return "application_add2";

        }else {
            application.setApplicationbsck("待补发");
            int update = applicationService.saveApplication(application1);
            msgs = "提交成功！";
            modelMap.put("msgs", msgs);
            model.addAttribute("applications", new Application());
            return "application_add2";


        }


    }
    @RequestMapping(value = "/save2",method = RequestMethod.POST)
    public  String saveApplication2(Application application){
        int update=applicationService.saveApplication(application);
        return "redirect:/applications/search2";
    }

    /**
     * 管理员审批管理--通过（默认未通过）
     *
     * @param id
     * @return
     */
    @RequestMapping("/pass2")
    @ResponseBody
    public String pass2(@RequestParam("id") String id) {
        Application application = applicationService.findApplicationById(id);
        application.setApplicationbsck("已补发");
        int update = applicationService.saveApplication(application);
        return "ok";
    }



    /**
     * 管理员添加审核意见
     * @param id
     * @param model
     * @return
     */

    @GetMapping("/amend2")
    public String amendApplication2(@RequestParam("id") String id,Model model){
        Application application=applicationService.findApplicationById(id);
        application.setUserId(application.getUserId());
        model.addAttribute("applications",application);
        return "application-amend2";
    }



}
